package com.woniu.project.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.project.core.domain.Shop;
import com.woniu.project.dto.shopdto.ShopDto;
import com.woniu.project.dao.ShopMapper;
import com.woniu.project.dto.storedto.StoreDto;
import com.woniu.project.params.shopparams.ShopConditionParams;
import com.woniu.project.params.shopparams.ShopParams;
import com.woniu.project.service.IShopService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.project.service.IShopStoreService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author WHF
 * @since 2020-10-12
 */
@Service
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private IShopStoreService shopStoreService;

    /**
     * 条件查询门店
     * @param shopParams 门店条件对象
     * @return
     */
    @Override
    public IPage<ShopDto> pageQuery(ShopConditionParams shopParams) {
        Page<ShopDto> page = new Page<>(shopParams.getPageIndex(),shopParams.getPageNumber());
        IPage<ShopDto> shopDtoIPage =  shopMapper.pageQueryShopDto(page,shopParams.getStatus(),shopParams.getShopParams());
        List<ShopDto> records = shopDtoIPage.getRecords();
        for(ShopDto shopDto:records){
            List<StoreDto> list = shopMapper.slectStore(shopDto.getShopId());
            shopDto.setStore(list);
        }
        shopDtoIPage.setRecords(records);
        return shopDtoIPage;
    }

    /**
     * 根据id查询门店
     * @param shopId 门店id
     * @return
     */
    @Override
    public ShopDto selectOneShop(int shopId) {
        ShopDto shopDto = shopMapper.selectOneShop(shopId);
        List<StoreDto> storeDtos = shopMapper.slectStore(shopId);
        shopDto.setStore(storeDtos);
        return shopDto;
    }

    /**
     * 根据门店id删除门店
     * @param shopId 门店id
     */
    @Override
    @Transactional
    public void deleteOneShop(int shopId) {
        shopMapper.deleteById(shopId);
        shopStoreService.deleteStores(shopId);
    }

    /**
     * 新增/修改 门店
     * @param shopParams 门店对象
     */
    @Override
    @Transactional
    public void updateShop(ShopParams shopParams) {
        System.out.println(shopParams);
        Shop shop=new Shop();
        BeanUtils.copyProperties(shopParams,shop);
        System.out.println(shop);
        saveOrUpdate(shop);
        if(shopParams.getShopId()!=null){
            shopStoreService.deleteStores(shopParams.getShopId());
            Integer[] storeIds = shopParams.getStoreIds();

            for(Integer storeId:storeIds){
                shopStoreService.insertShopStore(shop.getShopId(),storeId);
            }
        }

    }
}
